using System.Collections.Generic;
using Engine.Particles.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Engine.Particles
{
    public class ParticleEngingeService : IParticleEngine
    {
        public List<IParticleEffect> ParticleEffects { get; set; }

        public void AddParticleEffect(IParticleEffect particleEffect)
        {
            ParticleEffects.Add(particleEffect);
        }

        public ParticleEngingeService()
        {
            ParticleEffects = new List<IParticleEffect>();
        }

        public virtual void Update(GameTime gameTime)
        {
            foreach (var particleEffect in ParticleEffects)
            {
                particleEffect.Update(gameTime);
            }
            Cleanup();
            ParticleEffects.ForEach(x => x.Update(gameTime));
        }

        private void Cleanup()
        {
            var effects = new List<IParticleEffect>();
            foreach (var particleEffect in ParticleEffects)
            {
                if (particleEffect.HasEnded)
                    effects.Add(particleEffect);
            }
            effects.ForEach(x => ParticleEffects.Remove(x));
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            ParticleEffects.ForEach(x => x.Draw(spriteBatch));
        }
    }
}